package com.yzg.executors;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author misterWei
 * @create 2020年06月13号:02点55分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.ExecutorsCommit")
public class ExecutorsCommit {

    //测试提交任务
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        // 提交任务执行方式-- 有结果返回值
        Future<String> stringFuture = executorService.submit(() -> "1");
        System.out.println(stringFuture.get());

        //提交任务方式-- 普通执行
        executorService.execute(()->{log.info("普通执行");});

        // 提交任务方式-- 任务列表执行
        List<Future<String>> futures = executorService.invokeAll(Arrays.asList(
                () -> "1",
                () -> "2",
                () -> "3"
        ));
        futures.forEach((f)-> {
            try {
                System.out.println(f.get()+"---"+Thread.currentThread().getName());
            } catch (InterruptedException | ExecutionException e) {

            }
        });

        //提交任务方式 -- 从多个任务列表中返回一次,其它终止执行
        String invokeAny = executorService.invokeAny(Arrays.asList(
                () -> "1",
                () -> "2",
                () -> "3"
        ));
        System.out.println(invokeAny);

    }
}
